package Servlet;

import Pojo.User;
import Service.Serviceimpl.Manageserviceimpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
//获取用户账号信息表
public class Manageaddrootservlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/http;charset=UTF-8");
        Manageserviceimpl manageimpl = new Manageserviceimpl();
        List<User> user = manageimpl.getusermanage();
        Map<String,Object> map =new HashMap<String,Object>();
        if (user.size()!=0){
            map.put("code",0);
            map.put("msg","获取信息成功");
            map.put("count",user.size());
            map.put("data",user);
        }
        Gson gson = new Gson();
        String s = gson.toJson(map);
        resp.getWriter().write(s);
    }
//添加用户账号信息表
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       req.setCharacterEncoding("utf-8");
       int id = Integer.parseInt(req.getParameter("id"));
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String states =null;
         states= Objects.equals(req.getParameter("switch"), "on")? "1": "0";
         int state=Integer.parseInt(states);
        User user = new User(id,username, password, state);
        boolean b = new Manageserviceimpl().addUserManage(user);
        if (b){
           resp.getWriter().write("ok");
        }
    }
}
